<template>
    <div style="width: 100%; height: 100vh; background-color: #323739; overflow: hidden">
        <div style="width: 400px; margin: 150px auto">
            <div style="color: #cccccc; font-size: 30px; text-align: center; padding: 30px 0">欢迎注册</div>
            <el-form ref="form" :model="form" size="normal" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="Avatar" placeholder="用户名" v-model="form.username"/>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="Lock" v-model="form.password" placeholder="设置密码" show-password/>
                </el-form-item>
                <el-form-item prop="confirm">
                    <el-input prefix-icon="Lock" v-model="form.confirm" placeholder="确认密码" show-password/>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="register('form')">注 册</el-button>
                </el-form-item>
                <el-form-item>
                    <el-link style="width: 15%; margin: auto" type="primary" @click="this.$router.push('/login')">去登录</el-link>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import api from "@/api";
    import {ElMessage} from "_element-plus@2.2.18@element-plus";

    export default {
        name: "Register",
        data() {
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 1, max: 6, message: '用户名长度1至6', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请设置密码', trigger: 'blur'},
                        {min: 6, max: 18, message: '用户名长度6至18', trigger: 'blur'},
                    ],
                    confirm: [
                        {required: true, message: '请确认密码', trigger: 'blur'},
                    ]
                },
            }
        },
        methods: {
            register(form) {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        if (this.form.password !== this.form.confirm) {
                            ElMessage.error("两次密码输入不一致")
                            return
                        }
                        api.register(this.form).then(res => {
                            if (res.data.code == "0") {
                                ElMessage({
                                    message: '注册成功',
                                    type: 'success'
                                })
                                this.$router.push("/login")       // 注册成功后跳转到登录页
                            } else {
                                ElMessage.error(res.data.msg)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            }
        },
    }
</script>

<style scoped>

</style>